import {goodslist,goodscount} from "../../request/api.js"

const state={
    list:[],
    total:0,
    size:2,
    page:1
}
const mutations={
    changeList(state,arr){
        state.list=arr
    },
    changeTatol(state,num){
        state.total=num
    },
    changePage(state,num){
        state.page=num
    },
}
const actions={
    asyncChangeList(context){
        goodslist({page:context.state.page,size:context.state.size}).then(res=>{
            let arr =res.data.list?res.data.list:[]
            if(arr.length<=0&& context.state.page>1){
                context.commit("changePage",context.state.page-1)
                context.dispatch("asyncChangeList")
            }
            context.commit("changeList",arr)
        })    
    },
    asyncChangeTotal(context){
        goodscount().then(res=>{
context.commit("changeTatol",res.data.list[0].total)
        })      
    },
    asyncChangePage(context,num){
        context.commit("changePage",num)
        context.dispatch("asyncChangeList")
    }
}
const getters={
    list(state){
        return state.list
    },
    total(state){
        return state.total
    },
    size(state){
        return state.size
    }
}

export default{
    state,mutations,actions,getters,namespaced:true
}
